
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.models.fields.related &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-models-fields-related">
            
  <h1>Source code for django.db.models.fields.related</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">partial</span>

<span class="kn">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">SettingsReference</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="k">import</span> <span class="n">checks</span><span class="p">,</span> <span class="n">exceptions</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">connection</span><span class="p">,</span> <span class="n">router</span>
<span class="kn">from</span> <span class="nn">django.db.backends</span> <span class="k">import</span> <span class="n">utils</span>
<span class="kn">from</span> <span class="nn">django.db.models</span> <span class="k">import</span> <span class="n">Q</span>
<span class="kn">from</span> <span class="nn">django.db.models.constants</span> <span class="k">import</span> <span class="n">LOOKUP_SEP</span>
<span class="kn">from</span> <span class="nn">django.db.models.deletion</span> <span class="k">import</span> <span class="n">CASCADE</span><span class="p">,</span> <span class="n">SET_DEFAULT</span><span class="p">,</span> <span class="n">SET_NULL</span>
<span class="kn">from</span> <span class="nn">django.db.models.query_utils</span> <span class="k">import</span> <span class="n">PathInfo</span>
<span class="kn">from</span> <span class="nn">django.db.models.utils</span> <span class="k">import</span> <span class="n">make_model_tuple</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">cached_property</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">gettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">Field</span>
<span class="kn">from</span> <span class="nn">.mixins</span> <span class="k">import</span> <span class="n">FieldCacheMixin</span>
<span class="kn">from</span> <span class="nn">.related_descriptors</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">ForwardManyToOneDescriptor</span><span class="p">,</span> <span class="n">ForwardOneToOneDescriptor</span><span class="p">,</span>
    <span class="n">ManyToManyDescriptor</span><span class="p">,</span> <span class="n">ReverseManyToOneDescriptor</span><span class="p">,</span>
    <span class="n">ReverseOneToOneDescriptor</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">.related_lookups</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">RelatedExact</span><span class="p">,</span> <span class="n">RelatedGreaterThan</span><span class="p">,</span> <span class="n">RelatedGreaterThanOrEqual</span><span class="p">,</span> <span class="n">RelatedIn</span><span class="p">,</span>
    <span class="n">RelatedIsNull</span><span class="p">,</span> <span class="n">RelatedLessThan</span><span class="p">,</span> <span class="n">RelatedLessThanOrEqual</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">.reverse_related</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">ForeignObjectRel</span><span class="p">,</span> <span class="n">ManyToManyRel</span><span class="p">,</span> <span class="n">ManyToOneRel</span><span class="p">,</span> <span class="n">OneToOneRel</span><span class="p">,</span>
<span class="p">)</span>

<span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span> <span class="o">=</span> <span class="s1">&#39;self&#39;</span>


<span class="k">def</span> <span class="nf">resolve_relation</span><span class="p">(</span><span class="n">scope_model</span><span class="p">,</span> <span class="n">relation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Transform relation into a model or fully-qualified model string of the form</span>
<span class="sd">    &quot;app_label.ModelName&quot;, relative to scope_model.</span>

<span class="sd">    The relation argument can be:</span>
<span class="sd">      * RECURSIVE_RELATIONSHIP_CONSTANT, i.e. the string &quot;self&quot;, in which case</span>
<span class="sd">        the model argument will be returned.</span>
<span class="sd">      * A bare model name without an app_label, in which case scope_model&#39;s</span>
<span class="sd">        app_label will be prepended.</span>
<span class="sd">      * An &quot;app_label.ModelName&quot; string.</span>
<span class="sd">      * A model class, which will be returned unchanged.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Check for recursive relations</span>
    <span class="k">if</span> <span class="n">relation</span> <span class="o">==</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">:</span>
        <span class="n">relation</span> <span class="o">=</span> <span class="n">scope_model</span>

    <span class="c1"># Look for an &quot;app.Model&quot; relation</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">relation</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">if</span> <span class="s2">&quot;.&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">relation</span><span class="p">:</span>
            <span class="n">relation</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">scope_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">relation</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">relation</span>


<span class="k">def</span> <span class="nf">lazy_related_operation</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">*</span><span class="n">related_models</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Schedule `function` to be called once `model` and all `related_models`</span>
<span class="sd">    have been imported and registered with the app registry. `function` will</span>
<span class="sd">    be called with the newly-loaded model classes as its positional arguments,</span>
<span class="sd">    plus any optional keyword arguments.</span>

<span class="sd">    The `model` argument must be a model class. Each subsequent positional</span>
<span class="sd">    argument is another model, or a reference to another model - see</span>
<span class="sd">    `resolve_relation()` for the various forms these may take. Any relative</span>
<span class="sd">    references will be resolved relative to `model`.</span>

<span class="sd">    This is a convenience wrapper for `Apps.lazy_model_operation` - the app</span>
<span class="sd">    registry model used is the one found in `model._meta.apps`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">models</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">resolve_relation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">rel</span><span class="p">)</span> <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">related_models</span><span class="p">]</span>
    <span class="n">model_keys</span> <span class="o">=</span> <span class="p">(</span><span class="n">make_model_tuple</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">models</span><span class="p">)</span>
    <span class="n">apps</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span>
    <span class="k">return</span> <span class="n">apps</span><span class="o">.</span><span class="n">lazy_model_operation</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="o">*</span><span class="n">model_keys</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">RelatedField</span><span class="p">(</span><span class="n">FieldCacheMixin</span><span class="p">,</span> <span class="n">Field</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class that all relational fields inherit from.&quot;&quot;&quot;</span>

    <span class="c1"># Field flags</span>
    <span class="n">one_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_one</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">many_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">many_to_one</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">related_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Can&#39;t cache this property until all the models are loaded.</span>
        <span class="n">apps</span><span class="o">.</span><span class="n">check_models_ready</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="o">*</span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_related_name_is_valid</span><span class="p">(),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_related_query_name_is_valid</span><span class="p">(),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_relation_model_exists</span><span class="p">(),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_referencing_to_swapped_model</span><span class="p">(),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_clashes</span><span class="p">(),</span>
        <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_check_related_name_is_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">keyword</span>
        <span class="n">related_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span>
        <span class="k">if</span> <span class="n">related_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">is_valid_id</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">keyword</span><span class="o">.</span><span class="n">iskeyword</span><span class="p">(</span><span class="n">related_name</span><span class="p">)</span> <span class="ow">and</span> <span class="n">related_name</span><span class="o">.</span><span class="n">isidentifier</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">is_valid_id</span> <span class="ow">or</span> <span class="n">related_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;+&#39;</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;The name &#39;</span><span class="si">%s</span><span class="s2">&#39; is invalid related_name for field </span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">),</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;Related name must be a valid Python identifier or end with a &#39;+&#39;&quot;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E306&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_related_query_name_is_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">():</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">rel_query_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">()</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">rel_query_name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">):</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;Reverse query name &#39;</span><span class="si">%s</span><span class="s2">&#39; must not end with an underscore.&quot;</span>
                    <span class="o">%</span> <span class="p">(</span><span class="n">rel_query_name</span><span class="p">,),</span>
                    <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;Add or change a related_name or related_query_name &quot;</span>
                          <span class="s2">&quot;argument for this field.&quot;</span><span class="p">),</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E308&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">LOOKUP_SEP</span> <span class="ow">in</span> <span class="n">rel_query_name</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;Reverse query name &#39;</span><span class="si">%s</span><span class="s2">&#39; must not contain &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span>
                    <span class="o">%</span> <span class="p">(</span><span class="n">rel_query_name</span><span class="p">,</span> <span class="n">LOOKUP_SEP</span><span class="p">),</span>
                    <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;Add or change a related_name or related_query_name &quot;</span>
                          <span class="s2">&quot;argument for this field.&quot;</span><span class="p">),</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E309&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_relation_model_exists</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rel_is_missing</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">()</span>
        <span class="n">rel_is_string</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
        <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="k">if</span> <span class="n">rel_is_string</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
        <span class="k">if</span> <span class="n">rel_is_missing</span> <span class="ow">and</span> <span class="p">(</span><span class="n">rel_is_string</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;Field defines a relation with model &#39;</span><span class="si">%s</span><span class="s2">&#39;, which is either &quot;</span>
                    <span class="s2">&quot;not installed, or is abstract.&quot;</span> <span class="o">%</span> <span class="n">model_name</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E300&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_referencing_to_swapped_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">()</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">):</span>
            <span class="n">model</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;Field defines a relation with the model &#39;</span><span class="si">%s</span><span class="s2">&#39;, which has &quot;</span>
                    <span class="s2">&quot;been swapped out.&quot;</span> <span class="o">%</span> <span class="n">model</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;Update the relation to point at &#39;settings.</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E301&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_clashes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check accessor and reverse query name clashes.&quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.db.models.base</span> <span class="k">import</span> <span class="n">ModelBase</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>

        <span class="c1"># `f.remote_field.model` may be a string instead of a model. Skip if model name is</span>
        <span class="c1"># not resolved.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">ModelBase</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="c1"># Consider that we are checking field `Model.foreign` and the models</span>
        <span class="c1"># are:</span>
        <span class="c1">#</span>
        <span class="c1">#     class Target(models.Model):</span>
        <span class="c1">#         model = models.IntegerField()</span>
        <span class="c1">#         model_set = models.IntegerField()</span>
        <span class="c1">#</span>
        <span class="c1">#     class Model(models.Model):</span>
        <span class="c1">#         foreign = models.ForeignKey(Target)</span>
        <span class="c1">#         m2m = models.ManyToManyField(Target)</span>

        <span class="c1"># rel_opts.object_name == &quot;Target&quot;</span>
        <span class="n">rel_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="c1"># If the field doesn&#39;t install a backward relation on the target model</span>
        <span class="c1"># (so `is_hidden` returns True), then there are no clashes to check</span>
        <span class="c1"># and we can skip these fields.</span>
        <span class="n">rel_is_hidden</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">()</span>
        <span class="n">rel_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span>  <span class="c1"># i. e. &quot;model_set&quot;</span>
        <span class="n">rel_query_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">()</span>  <span class="c1"># i. e. &quot;model&quot;</span>
        <span class="n">field_name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>  <span class="c1"># i. e. &quot;Model.field&quot;</span>

        <span class="c1"># Check clashes between accessor or reverse query name of `field`</span>
        <span class="c1"># and any other field name -- i.e. accessor for Model.foreign is</span>
        <span class="c1"># model_set and it clashes with Target.model_set.</span>
        <span class="n">potential_clashes</span> <span class="o">=</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">fields</span> <span class="o">+</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">many_to_many</span>
        <span class="k">for</span> <span class="n">clash_field</span> <span class="ow">in</span> <span class="n">potential_clashes</span><span class="p">:</span>
            <span class="n">clash_name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">rel_opts</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>  <span class="c1"># i.e. &quot;Target.model_set&quot;</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">rel_is_hidden</span> <span class="ow">and</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">rel_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Reverse accessor for &#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with field name &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;Rename field &#39;</span><span class="si">%s</span><span class="s2">&#39;, or add/change a related_name &quot;</span>
                              <span class="s2">&quot;argument to the definition for field &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">clash_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E302&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">if</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">rel_query_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Reverse query name for &#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with field name &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;Rename field &#39;</span><span class="si">%s</span><span class="s2">&#39;, or add/change a related_name &quot;</span>
                              <span class="s2">&quot;argument to the definition for field &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">clash_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E303&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="c1"># Check clashes between accessors/reverse query names of `field` and</span>
        <span class="c1"># any other field accessor -- i. e. Model.foreign accessor clashes with</span>
        <span class="c1"># Model.m2m accessor.</span>
        <span class="n">potential_clashes</span> <span class="o">=</span> <span class="p">(</span><span class="n">r</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rel_opts</span><span class="o">.</span><span class="n">related_objects</span> <span class="k">if</span> <span class="n">r</span><span class="o">.</span><span class="n">field</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">clash_field</span> <span class="ow">in</span> <span class="n">potential_clashes</span><span class="p">:</span>
            <span class="n">clash_name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>  <span class="c1"># i. e. &quot;Model.m2m&quot;</span>
                <span class="n">clash_field</span><span class="o">.</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="n">clash_field</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">rel_is_hidden</span> <span class="ow">and</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span> <span class="o">==</span> <span class="n">rel_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Reverse accessor for &#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with reverse accessor for &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;Add or change a related_name argument &quot;</span>
                              <span class="s2">&quot;to the definition for &#39;</span><span class="si">%s</span><span class="s2">&#39; or &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E304&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="k">if</span> <span class="n">clash_field</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">()</span> <span class="o">==</span> <span class="n">rel_query_name</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Reverse query name for &#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with reverse query name for &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">hint</span><span class="o">=</span><span class="p">(</span><span class="s2">&quot;Add or change a related_name argument &quot;</span>
                              <span class="s2">&quot;to the definition for &#39;</span><span class="si">%s</span><span class="s2">&#39; or &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">clash_name</span><span class="p">),</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E305&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="c1"># By default related field will not have a column as it relates to</span>
        <span class="c1"># columns from another table.</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">private_only</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">private_only</span><span class="o">=</span><span class="n">private_only</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span><span class="p">:</span>
                <span class="n">related_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">related_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">default_related_name</span>
            <span class="k">if</span> <span class="n">related_name</span><span class="p">:</span>
                <span class="n">related_name</span> <span class="o">=</span> <span class="n">related_name</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
                    <span class="s1">&#39;model_name&#39;</span><span class="p">:</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
                    <span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                <span class="p">}</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="n">related_name</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_query_name</span><span class="p">:</span>
                <span class="n">related_query_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_query_name</span> <span class="o">%</span> <span class="p">{</span>
                    <span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
                    <span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span>
                <span class="p">}</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_query_name</span> <span class="o">=</span> <span class="n">related_query_name</span>

            <span class="k">def</span> <span class="nf">resolve_related_class</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
                <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">related</span>
                <span class="n">field</span><span class="o">.</span><span class="n">do_related_class</span><span class="p">(</span><span class="n">related</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
            <span class="n">lazy_related_operation</span><span class="p">(</span><span class="n">resolve_related_class</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;limit_choices_to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;related_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_query_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;related_query_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_query_name</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">get_forward_related_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the keyword arguments that when supplied to</span>
<span class="sd">        self.model.object.filter(), would select all instances related through</span>
<span class="sd">        this field to the remote obj. This is used to build the querysets</span>
<span class="sd">        returned by related descriptors. obj is an instance of</span>
<span class="sd">        self.related_field.model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">__</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">name</span><span class="p">):</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">rh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">rh_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">get_reverse_related_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Complement to get_forward_related_filter(). Return the keyword</span>
<span class="sd">        arguments that when passed to self.related_field.model.object.filter()</span>
<span class="sd">        select all instances of self.related_field.model related through</span>
<span class="sd">        this field to obj. obj is an instance of self.model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">base_filter</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">rh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">:</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">lh_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">lh_field</span><span class="p">,</span> <span class="n">rh_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span>
        <span class="p">}</span>
        <span class="n">descriptor_filter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_extra_descriptor_filter</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="n">base_q</span> <span class="o">=</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="n">base_filter</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">descriptor_filter</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">base_q</span> <span class="o">&amp;</span> <span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="n">descriptor_filter</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">descriptor_filter</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">base_q</span> <span class="o">&amp;</span> <span class="n">descriptor_filter</span>
        <span class="k">return</span> <span class="n">base_q</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">swappable_setting</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the setting that this is powered from for swapping, or None</span>
<span class="sd">        if it&#39;s not swapped in / marked with swappable=False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span><span class="p">:</span>
            <span class="c1"># Work out string form of &quot;to&quot;</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="n">to_string</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">to_string</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span>
            <span class="k">return</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_swappable_settings_name</span><span class="p">(</span><span class="n">to_string</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">set_attributes_from_rel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="ow">or</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">set_field_name</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">do_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_attributes_from_rel</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contribute_to_related_class</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_limit_choices_to</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return ``limit_choices_to`` for this model field.</span>

<span class="sd">        If it is a callable, it will be invoked and the result will be</span>
<span class="sd">        returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pass ``limit_choices_to`` to the field being constructed.</span>

<span class="sd">        Only passes it if there is a type that supports related fields.</span>
<span class="sd">        This is a similar strategy used to pass the ``queryset`` to the field</span>
<span class="sd">        being constructed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;get_related_field&#39;</span><span class="p">):</span>
            <span class="c1"># If this is a callable, do not invoke it here. Just pass</span>
            <span class="c1"># it in the defaults for when the form class will later be</span>
            <span class="c1"># instantiated.</span>
            <span class="n">limit_choices_to</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span>
            <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
                <span class="s1">&#39;limit_choices_to&#39;</span><span class="p">:</span> <span class="n">limit_choices_to</span><span class="p">,</span>
            <span class="p">})</span>
        <span class="n">defaults</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">related_query_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Define the name that can be used to identify this related object in a</span>
<span class="sd">        table-spanning query.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_query_name</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">model_name</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        When filtering against this relation, return the field on the remote</span>
<span class="sd">        model against which the filtering should happen.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">target_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">target_fields</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">target_fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldError</span><span class="p">(</span>
                <span class="s2">&quot;The relation has multiple target fields, but only single target field was asked for&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">target_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_cache_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>


<span class="k">class</span> <span class="nc">ForeignObject</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Abstraction of the ForeignKey relation to support multi-column relations.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Field flags</span>
    <span class="n">many_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">many_to_one</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">one_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_one</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="n">requires_unique_target</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">related_accessor_class</span> <span class="o">=</span> <span class="n">ReverseManyToOneDescriptor</span>
    <span class="n">forward_related_accessor_class</span> <span class="o">=</span> <span class="n">ForwardManyToOneDescriptor</span>
    <span class="n">rel_class</span> <span class="o">=</span> <span class="n">ForeignObjectRel</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">on_delete</span><span class="p">,</span> <span class="n">from_fields</span><span class="p">,</span> <span class="n">to_fields</span><span class="p">,</span> <span class="n">rel</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">related_query_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_link</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">swappable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="k">if</span> <span class="n">rel</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">rel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel_class</span><span class="p">(</span>
                <span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span>
                <span class="n">related_name</span><span class="o">=</span><span class="n">related_name</span><span class="p">,</span>
                <span class="n">related_query_name</span><span class="o">=</span><span class="n">related_query_name</span><span class="p">,</span>
                <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">limit_choices_to</span><span class="p">,</span>
                <span class="n">parent_link</span><span class="o">=</span><span class="n">parent_link</span><span class="p">,</span>
                <span class="n">on_delete</span><span class="o">=</span><span class="n">on_delete</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">rel</span><span class="o">=</span><span class="n">rel</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span> <span class="o">=</span> <span class="n">from_fields</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span> <span class="o">=</span> <span class="n">to_fields</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span> <span class="o">=</span> <span class="n">swappable</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="o">*</span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_to_fields_exist</span><span class="p">(),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_unique_target</span><span class="p">(),</span>
        <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_check_to_fields_exist</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Skip nonexistent models.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">to_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">to_field</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">to_field</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldDoesNotExist</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;The to_field &#39;</span><span class="si">%s</span><span class="s2">&#39; doesn&#39;t exist on the related &quot;</span>
                            <span class="s2">&quot;model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span>
                            <span class="o">%</span> <span class="p">(</span><span class="n">to_field</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span><span class="p">),</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E312&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_unique_target</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rel_is_string</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rel_is_string</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">requires_unique_target</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span>
        <span class="k">except</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldDoesNotExist</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="n">unique_foreign_fields</span> <span class="o">=</span> <span class="p">{</span>
            <span class="nb">frozenset</span><span class="p">([</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;unique&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="p">}</span>
        <span class="n">unique_foreign_fields</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
            <span class="nb">frozenset</span><span class="p">(</span><span class="n">ut</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">ut</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">unique_together</span>
        <span class="p">})</span>
        <span class="n">foreign_fields</span> <span class="o">=</span> <span class="p">{</span><span class="n">f</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">}</span>
        <span class="n">has_unique_constraint</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span><span class="n">u</span> <span class="o">&lt;=</span> <span class="n">foreign_fields</span> <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">unique_foreign_fields</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">has_unique_constraint</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">field_combination</span> <span class="o">=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
                <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="n">rel_field</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">rel_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span>
            <span class="p">)</span>
            <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;No subset of the fields </span><span class="si">%s</span><span class="s2"> on model &#39;</span><span class="si">%s</span><span class="s2">&#39; is unique.&quot;</span>
                    <span class="o">%</span> <span class="p">(</span><span class="n">field_combination</span><span class="p">,</span> <span class="n">model_name</span><span class="p">),</span>
                    <span class="n">hint</span><span class="o">=</span><span class="p">(</span>
                        <span class="s2">&quot;Add unique=True on any of those fields or add at &quot;</span>
                        <span class="s2">&quot;least a subset of them to a unique_together constraint.&quot;</span>
                    <span class="p">),</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E310&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">has_unique_constraint</span><span class="p">:</span>
            <span class="n">field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
            <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&#39; must set unique=True because it is referenced by &quot;</span>
                    <span class="s2">&quot;a foreign key.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E311&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;on_delete&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">on_delete</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;from_fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to_fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;parent_link&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">parent_link</span>
        <span class="c1"># Work out string form of &quot;to&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="c1"># If swappable is True, then see if we&#39;re actually pointing to the target</span>
        <span class="c1"># of a swap.</span>
        <span class="n">swappable_setting</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable_setting</span>
        <span class="k">if</span> <span class="n">swappable_setting</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># If it&#39;s already a settings reference, error</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">],</span> <span class="s2">&quot;setting_name&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span> <span class="o">!=</span> <span class="n">swappable_setting</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;Cannot deconstruct a ForeignKey pointing to a model &quot;</span>
                        <span class="s2">&quot;that is swapped in place of more than one model (</span><span class="si">%s</span><span class="s2"> and </span><span class="si">%s</span><span class="s2">)&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span><span class="p">,</span> <span class="n">swappable_setting</span><span class="p">)</span>
                    <span class="p">)</span>
            <span class="c1"># Set it</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">SettingsReference</span><span class="p">(</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">],</span>
                <span class="n">swappable_setting</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">resolve_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Foreign Object from and to fields must be the same non-zero length&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Related model </span><span class="si">%r</span><span class="s1"> cannot be resolved&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="n">related_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">)):</span>
            <span class="n">from_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">from_fields</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
            <span class="n">to_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_fields</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
            <span class="n">from_field</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span> <span class="k">if</span> <span class="n">from_field_name</span> <span class="o">==</span> <span class="s1">&#39;self&#39;</span>
                          <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">from_field_name</span><span class="p">))</span>
            <span class="n">to_field</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span> <span class="k">if</span> <span class="n">to_field_name</span> <span class="ow">is</span> <span class="kc">None</span>
                        <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">to_field_name</span><span class="p">))</span>
            <span class="n">related_fields</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">from_field</span><span class="p">,</span> <span class="n">to_field</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">related_fields</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_related_fields&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_related_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolve_related_fields</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_related_fields</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reverse_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[(</span><span class="n">rhs_field</span><span class="p">,</span> <span class="n">lhs_field</span><span class="p">)</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">local_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">lhs_field</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">foreign_related_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">rhs_field</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span> <span class="k">if</span> <span class="n">rhs_field</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_local_related_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_instance_value_for_fields</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">local_related_fields</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_foreign_related_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_instance_value_for_fields</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">get_instance_value_for_fields</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">fields</span><span class="p">):</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="c1"># Gotcha: in some cases (like fixture loading) a model can have</span>
            <span class="c1"># different values in parent_ptr_id and parent&#39;s id. So, use</span>
            <span class="c1"># instance.pk (that is, parent_ptr_id) when asked for instance.id.</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
                <span class="n">possible_parent_link</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_ancestor_link</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">possible_parent_link</span> <span class="ow">or</span>
                        <span class="n">possible_parent_link</span><span class="o">.</span><span class="n">primary_key</span> <span class="ow">or</span>
                        <span class="n">possible_parent_link</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">):</span>
                    <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">pk</span><span class="p">)</span>
                    <span class="k">continue</span>
            <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_attname_column</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">attname</span><span class="p">,</span> <span class="n">column</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_attname_column</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">attname</span><span class="p">,</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">get_joining_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reverse_join</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">source</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_related_fields</span> <span class="k">if</span> <span class="n">reverse_join</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_fields</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">((</span><span class="n">lhs_field</span><span class="o">.</span><span class="n">column</span><span class="p">,</span> <span class="n">rhs_field</span><span class="o">.</span><span class="n">column</span><span class="p">)</span> <span class="k">for</span> <span class="n">lhs_field</span><span class="p">,</span> <span class="n">rhs_field</span> <span class="ow">in</span> <span class="n">source</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_reverse_joining_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_joining_columns</span><span class="p">(</span><span class="n">reverse_join</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_extra_descriptor_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return an extra filter condition for related object fetching when</span>
<span class="sd">        user does &#39;instance.fieldname&#39;, that is the extra filter is used in</span>
<span class="sd">        the descriptor of the field.</span>

<span class="sd">        The filter should be either a dict usable in .filter(**kwargs) call or</span>
<span class="sd">        a Q-object. The condition will be ANDed together with the relation&#39;s</span>
<span class="sd">        joining columns.</span>

<span class="sd">        A parallel method is get_extra_restriction() which is used in</span>
<span class="sd">        JOIN and subquery conditions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">get_extra_restriction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">where_class</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">related_alias</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a pair condition used for joining and subquery pushdown. The</span>
<span class="sd">        condition is something that responds to as_sql(compiler, connection)</span>
<span class="sd">        method.</span>

<span class="sd">        Note that currently referring both the &#39;alias&#39; and &#39;related_alias&#39;</span>
<span class="sd">        will not work in some conditions, like subquery pushdown.</span>

<span class="sd">        A parallel method is get_extra_descriptor_filter() which is used in</span>
<span class="sd">        instance.fieldname related object fetching.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">get_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get path from this field to the related model.&quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">from_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">PathInfo</span><span class="p">(</span>
            <span class="n">from_opts</span><span class="o">=</span><span class="n">from_opts</span><span class="p">,</span>
            <span class="n">to_opts</span><span class="o">=</span><span class="n">opts</span><span class="p">,</span>
            <span class="n">target_fields</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">,</span>
            <span class="n">join_field</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
            <span class="n">m2m</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="n">direct</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="n">filtered_relation</span><span class="o">=</span><span class="n">filtered_relation</span><span class="p">,</span>
        <span class="p">)]</span>

    <span class="k">def</span> <span class="nf">get_reverse_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get path from the related model to this field&#39;s model.&quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">from_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">PathInfo</span><span class="p">(</span>
            <span class="n">from_opts</span><span class="o">=</span><span class="n">from_opts</span><span class="p">,</span>
            <span class="n">to_opts</span><span class="o">=</span><span class="n">opts</span><span class="p">,</span>
            <span class="n">target_fields</span><span class="o">=</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="p">,),</span>
            <span class="n">join_field</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span>
            <span class="n">m2m</span><span class="o">=</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span><span class="p">,</span>
            <span class="n">direct</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="n">filtered_relation</span><span class="o">=</span><span class="n">filtered_relation</span><span class="p">,</span>
        <span class="p">)]</span>

    <span class="nd">@classmethod</span>
    <span class="nd">@functools</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_lookups</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="n">bases</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getmro</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
        <span class="n">bases</span> <span class="o">=</span> <span class="n">bases</span><span class="p">[:</span><span class="n">bases</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">class_lookups</span> <span class="o">=</span> <span class="p">[</span><span class="n">parent</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;class_lookups&#39;</span><span class="p">,</span> <span class="p">{})</span> <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="n">bases</span><span class="p">]</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">merge_dicts</span><span class="p">(</span><span class="n">class_lookups</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">private_only</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">private_only</span><span class="o">=</span><span class="n">private_only</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">forward_related_accessor_class</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">contribute_to_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="c1"># Internal FK&#39;s - i.e., those with a related name ending with &#39;+&#39; -</span>
        <span class="c1"># and swapped models don&#39;t get a related descriptor.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">related</span><span class="o">.</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="p">,</span> <span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_accessor_class</span><span class="p">(</span><span class="n">related</span><span class="p">))</span>
            <span class="c1"># While &#39;limit_choices_to&#39; might be a callable, simply pass</span>
            <span class="c1"># it along for later - this is too early because it&#39;s still</span>
            <span class="c1"># model load time.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">:</span>
                <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_fkey_lookups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span><span class="p">)</span>


<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedIn</span><span class="p">)</span>
<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedExact</span><span class="p">)</span>
<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedLessThan</span><span class="p">)</span>
<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedGreaterThan</span><span class="p">)</span>
<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedGreaterThanOrEqual</span><span class="p">)</span>
<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedLessThanOrEqual</span><span class="p">)</span>
<span class="n">ForeignObject</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RelatedIsNull</span><span class="p">)</span>


<div class="viewcode-block" id="ForeignKey"><a class="viewcode-back" href="../../../../../ref/models/fields.html#django.db.models.ForeignKey">[docs]</a><span class="k">class</span> <span class="nc">ForeignKey</span><span class="p">(</span><span class="n">ForeignObject</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provide a many-to-one relation by adding a column to the local model</span>
<span class="sd">    to hold the remote value.</span>

<span class="sd">    By default ForeignKey will target the pk of the remote model but this</span>
<span class="sd">    behavior can be changed by using the ``to_field`` argument.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Field flags</span>
    <span class="n">many_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">many_to_one</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">one_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_one</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="n">rel_class</span> <span class="o">=</span> <span class="n">ManyToOneRel</span>

    <span class="n">empty_strings_allowed</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">default_error_messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;invalid&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%(model)s</span><span class="s1"> instance with </span><span class="si">%(field)s</span><span class="s1"> </span><span class="si">%(value)r</span><span class="s1"> does not exist.&#39;</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Foreign Key (type determined by related field)&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">on_delete</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">limit_choices_to</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">parent_link</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">to_field</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">db_constraint</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="nb">str</span><span class="p">),</span> <span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">(</span><span class="si">%r</span><span class="s2">) is invalid. First parameter to ForeignKey must be &quot;</span>
                <span class="s2">&quot;either a model, a model name, or the string </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span>
                    <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># For backwards compatibility purposes, we need to *try* and set</span>
            <span class="c1"># the to_field during FK construction. It won&#39;t be guaranteed to</span>
            <span class="c1"># be correct until contribute_to_class is called. Refs #12190.</span>
            <span class="n">to_field</span> <span class="o">=</span> <span class="n">to_field</span> <span class="ow">or</span> <span class="p">(</span><span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span> <span class="ow">and</span> <span class="n">to</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;rel&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel_class</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">to_field</span><span class="p">,</span>
            <span class="n">related_name</span><span class="o">=</span><span class="n">related_name</span><span class="p">,</span>
            <span class="n">related_query_name</span><span class="o">=</span><span class="n">related_query_name</span><span class="p">,</span>
            <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">limit_choices_to</span><span class="p">,</span>
            <span class="n">parent_link</span><span class="o">=</span><span class="n">parent_link</span><span class="p">,</span>
            <span class="n">on_delete</span><span class="o">=</span><span class="n">on_delete</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;db_index&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">on_delete</span><span class="p">,</span> <span class="n">from_fields</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;self&#39;</span><span class="p">],</span> <span class="n">to_fields</span><span class="o">=</span><span class="p">[</span><span class="n">to_field</span><span class="p">],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span> <span class="o">=</span> <span class="n">db_constraint</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="o">*</span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_on_delete</span><span class="p">(),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_unique</span><span class="p">(),</span>
        <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_check_on_delete</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">on_delete</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;on_delete&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">on_delete</span> <span class="o">==</span> <span class="n">SET_NULL</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">null</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s1">&#39;Field specifies on_delete=SET_NULL, but cannot be null.&#39;</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s1">&#39;Set null=True argument on the field, or change the on_delete rule.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E320&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="n">on_delete</span> <span class="o">==</span> <span class="n">SET_DEFAULT</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_default</span><span class="p">():</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s1">&#39;Field specifies on_delete=SET_DEFAULT, but has no default value.&#39;</span><span class="p">,</span>
                    <span class="n">hint</span><span class="o">=</span><span class="s1">&#39;Set a default value, or change the on_delete rule.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E321&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="n">checks</span><span class="o">.</span><span class="n">Warning</span><span class="p">(</span>
                <span class="s1">&#39;Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.&#39;</span><span class="p">,</span>
                <span class="n">hint</span><span class="o">=</span><span class="s1">&#39;ForeignKey(unique=True) is usually better served by a OneToOneField.&#39;</span><span class="p">,</span>
                <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.W342&#39;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span> <span class="k">else</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to_fields&#39;</span><span class="p">]</span>
        <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;from_fields&#39;</span><span class="p">]</span>
        <span class="c1"># Handle the simpler arguments</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_index</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;db_index&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;db_index&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;db_constraint&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_constraint</span>
        <span class="c1"># Rel needs more work.</span>
        <span class="n">to_meta</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="s2">&quot;_meta&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span> <span class="ow">and</span> <span class="p">(</span>
                <span class="ow">not</span> <span class="n">to_meta</span> <span class="ow">or</span> <span class="p">(</span><span class="n">to_meta</span><span class="o">.</span><span class="n">pk</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span> <span class="o">!=</span> <span class="n">to_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to_field&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">to_python</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">foreign_related_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_reverse_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get path from the related model to this field&#39;s model.&quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">from_opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">PathInfo</span><span class="p">(</span>
            <span class="n">from_opts</span><span class="o">=</span><span class="n">from_opts</span><span class="p">,</span>
            <span class="n">to_opts</span><span class="o">=</span><span class="n">opts</span><span class="p">,</span>
            <span class="n">target_fields</span><span class="o">=</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="p">,),</span>
            <span class="n">join_field</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span>
            <span class="n">m2m</span><span class="o">=</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span><span class="p">,</span>
            <span class="n">direct</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="n">filtered_relation</span><span class="o">=</span><span class="n">filtered_relation</span><span class="p">,</span>
        <span class="p">)]</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">model_instance</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">model_instance</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">using</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">model_instance</span><span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
            <span class="o">**</span><span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span><span class="p">:</span> <span class="n">value</span><span class="p">}</span>
        <span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">complex_filter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_limit_choices_to</span><span class="p">())</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">qs</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">error_messages</span><span class="p">[</span><span class="s1">&#39;invalid&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span>
                    <span class="s1">&#39;model&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span> <span class="s1">&#39;pk&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">,</span>
                    <span class="s1">&#39;field&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span><span class="p">,</span> <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">,</span>
                <span class="p">},</span>  <span class="c1"># &#39;pk&#39; is included for backwards compatibility</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_attname</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_id&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

    <span class="k">def</span> <span class="nf">get_attname_column</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">attname</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_attname</span><span class="p">()</span>
        <span class="n">column</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_column</span> <span class="ow">or</span> <span class="n">attname</span>
        <span class="k">return</span> <span class="n">attname</span><span class="p">,</span> <span class="n">column</span>

    <span class="k">def</span> <span class="nf">get_default</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the to_field if the default value is an object.&quot;&quot;&quot;</span>
        <span class="n">field_default</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_default</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field_default</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field_default</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">field_default</span>

    <span class="k">def</span> <span class="nf">get_db_prep_save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="p">(</span><span class="n">value</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span> <span class="ow">and</span>
                             <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">empty_strings_allowed</span> <span class="ow">or</span>
                              <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span><span class="p">)):</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">get_db_prep_save</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_db_prep_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">prepared</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">get_db_prep_value</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">prepared</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">contribute_to_related_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot create form field for </span><span class="si">%r</span><span class="s2"> yet, because &quot;</span>
                             <span class="s2">&quot;its related model </span><span class="si">%r</span><span class="s2"> has not been loaded yet&quot;</span> <span class="o">%</span>
                             <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
            <span class="s1">&#39;form_class&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelChoiceField</span><span class="p">,</span>
            <span class="s1">&#39;queryset&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">using</span><span class="p">),</span>
            <span class="s1">&#39;to_field_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">field_name</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">})</span>

    <span class="k">def</span> <span class="nf">db_check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span><span class="o">.</span><span class="n">rel_db_type</span><span class="p">(</span><span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">db_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_type</span><span class="p">(</span><span class="n">connection</span><span class="p">),</span> <span class="s2">&quot;check&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_check</span><span class="p">(</span><span class="n">connection</span><span class="p">)}</span>

    <span class="k">def</span> <span class="nf">convert_empty_strings</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">value</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">get_db_converters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="n">converters</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_db_converters</span><span class="p">(</span><span class="n">connection</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">interprets_empty_strings_as_nulls</span><span class="p">:</span>
            <span class="n">converters</span> <span class="o">+=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">convert_empty_strings</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">converters</span>

    <span class="k">def</span> <span class="nf">get_col</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">output_field</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">output_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_field</span>
            <span class="k">while</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">output_field</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">):</span>
                <span class="n">output_field</span> <span class="o">=</span> <span class="n">output_field</span><span class="o">.</span><span class="n">target_field</span>
                <span class="k">if</span> <span class="n">output_field</span> <span class="ow">is</span> <span class="bp">self</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Cannot resolve output_field.&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_col</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">output_field</span><span class="p">)</span></div>


<div class="viewcode-block" id="OneToOneField"><a class="viewcode-back" href="../../../../../ref/models/fields.html#django.db.models.OneToOneField">[docs]</a><span class="k">class</span> <span class="nc">OneToOneField</span><span class="p">(</span><span class="n">ForeignKey</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A OneToOneField is essentially the same as a ForeignKey, with the exception</span>
<span class="sd">    that it always carries a &quot;unique&quot; constraint with it and the reverse</span>
<span class="sd">    relation always returns the object pointed to (since there will only ever</span>
<span class="sd">    be one), rather than returning a list.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Field flags</span>
    <span class="n">many_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">many_to_one</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_one</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="n">related_accessor_class</span> <span class="o">=</span> <span class="n">ReverseOneToOneDescriptor</span>
    <span class="n">forward_related_accessor_class</span> <span class="o">=</span> <span class="n">ForwardOneToOneDescriptor</span>
    <span class="n">rel_class</span> <span class="o">=</span> <span class="n">OneToOneRel</span>

    <span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;One-to-one relationship&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">on_delete</span><span class="p">,</span> <span class="n">to_field</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;unique&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">on_delete</span><span class="p">,</span> <span class="n">to_field</span><span class="o">=</span><span class="n">to_field</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="k">if</span> <span class="s2">&quot;unique&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;unique&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">parent_link</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">save_form_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_check_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># Override ForeignKey since check isn&#39;t applicable here.</span>
        <span class="k">return</span> <span class="p">[]</span></div>


<span class="k">def</span> <span class="nf">create_many_to_many_intermediary_model</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">klass</span><span class="p">):</span>
    <span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

    <span class="k">def</span> <span class="nf">set_managed</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="n">through</span><span class="p">):</span>
        <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span> <span class="ow">or</span> <span class="n">related</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span>

    <span class="n">to_model</span> <span class="o">=</span> <span class="n">resolve_relation</span><span class="p">(</span><span class="n">klass</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
    <span class="n">lazy_related_operation</span><span class="p">(</span><span class="n">set_managed</span><span class="p">,</span> <span class="n">klass</span><span class="p">,</span> <span class="n">to_model</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="n">to</span> <span class="o">=</span> <span class="n">make_model_tuple</span><span class="p">(</span><span class="n">to_model</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">from_</span> <span class="o">=</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
    <span class="k">if</span> <span class="n">to</span> <span class="o">==</span> <span class="n">from_</span><span class="p">:</span>
        <span class="n">to</span> <span class="o">=</span> <span class="s1">&#39;to_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">to</span>
        <span class="n">from_</span> <span class="o">=</span> <span class="s1">&#39;from_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">from_</span>

    <span class="n">meta</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;Meta&#39;</span><span class="p">,</span> <span class="p">(),</span> <span class="p">{</span>
        <span class="s1">&#39;db_table&#39;</span><span class="p">:</span> <span class="n">field</span><span class="o">.</span><span class="n">_get_m2m_db_table</span><span class="p">(</span><span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="p">),</span>
        <span class="s1">&#39;auto_created&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="p">,</span>
        <span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
        <span class="s1">&#39;db_tablespace&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">,</span>
        <span class="s1">&#39;unique_together&#39;</span><span class="p">:</span> <span class="p">(</span><span class="n">from_</span><span class="p">,</span> <span class="n">to</span><span class="p">),</span>
        <span class="s1">&#39;verbose_name&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%(from)s</span><span class="s1">-</span><span class="si">%(to)s</span><span class="s1"> relationship&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;from&#39;</span><span class="p">:</span> <span class="n">from_</span><span class="p">,</span> <span class="s1">&#39;to&#39;</span><span class="p">:</span> <span class="n">to</span><span class="p">},</span>
        <span class="s1">&#39;verbose_name_plural&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%(from)s</span><span class="s1">-</span><span class="si">%(to)s</span><span class="s1"> relationships&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;from&#39;</span><span class="p">:</span> <span class="n">from_</span><span class="p">,</span> <span class="s1">&#39;to&#39;</span><span class="p">:</span> <span class="n">to</span><span class="p">},</span>
        <span class="s1">&#39;apps&#39;</span><span class="p">:</span> <span class="n">field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">apps</span><span class="p">,</span>
    <span class="p">})</span>
    <span class="c1"># Construct and return the new class.</span>
    <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">,),</span> <span class="p">{</span>
        <span class="s1">&#39;Meta&#39;</span><span class="p">:</span> <span class="n">meta</span><span class="p">,</span>
        <span class="s1">&#39;__module__&#39;</span><span class="p">:</span> <span class="n">klass</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
        <span class="n">from_</span><span class="p">:</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span>
            <span class="n">klass</span><span class="p">,</span>
            <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">+&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span>
            <span class="n">db_tablespace</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">,</span>
            <span class="n">db_constraint</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">,</span>
            <span class="n">on_delete</span><span class="o">=</span><span class="n">CASCADE</span><span class="p">,</span>
        <span class="p">),</span>
        <span class="n">to</span><span class="p">:</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span>
            <span class="n">to_model</span><span class="p">,</span>
            <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">+&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span>
            <span class="n">db_tablespace</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">db_tablespace</span><span class="p">,</span>
            <span class="n">db_constraint</span><span class="o">=</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">db_constraint</span><span class="p">,</span>
            <span class="n">on_delete</span><span class="o">=</span><span class="n">CASCADE</span><span class="p">,</span>
        <span class="p">)</span>
    <span class="p">})</span>


<div class="viewcode-block" id="ManyToManyField"><a class="viewcode-back" href="../../../../../ref/models/fields.html#django.db.models.ManyToManyField">[docs]</a><span class="k">class</span> <span class="nc">ManyToManyField</span><span class="p">(</span><span class="n">RelatedField</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provide a many-to-many relation by using an intermediary model that</span>
<span class="sd">    holds two ForeignKey fields pointed at the two sides of the relation.</span>

<span class="sd">    Unless a ``through`` model was provided, ManyToManyField will use the</span>
<span class="sd">    create_many_to_many_intermediary_model factory to automatically generate</span>
<span class="sd">    the intermediary model.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Field flags</span>
    <span class="n">many_to_many</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">many_to_one</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_many</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">one_to_one</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="n">rel_class</span> <span class="o">=</span> <span class="n">ManyToManyRel</span>

    <span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Many-to-many relationship&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">limit_choices_to</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">symmetrical</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">through_fields</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">db_constraint</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">db_table</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">swappable</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">to</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="nb">str</span><span class="p">),</span> <span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">(</span><span class="si">%r</span><span class="s2">) is invalid. First parameter to ManyToManyField must be &quot;</span>
                <span class="s2">&quot;either a model, a model name, or the string </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span>
                <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">symmetrical</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">symmetrical</span> <span class="o">=</span> <span class="p">(</span><span class="n">to</span> <span class="o">==</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">through</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">assert</span> <span class="n">db_table</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">,</span> <span class="p">(</span>
                <span class="s2">&quot;Cannot specify a db_table if an intermediary model is used.&quot;</span>
            <span class="p">)</span>

        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;rel&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rel_class</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span>
            <span class="n">related_name</span><span class="o">=</span><span class="n">related_name</span><span class="p">,</span>
            <span class="n">related_query_name</span><span class="o">=</span><span class="n">related_query_name</span><span class="p">,</span>
            <span class="n">limit_choices_to</span><span class="o">=</span><span class="n">limit_choices_to</span><span class="p">,</span>
            <span class="n">symmetrical</span><span class="o">=</span><span class="n">symmetrical</span><span class="p">,</span>
            <span class="n">through</span><span class="o">=</span><span class="n">through</span><span class="p">,</span>
            <span class="n">through_fields</span><span class="o">=</span><span class="n">through_fields</span><span class="p">,</span>
            <span class="n">db_constraint</span><span class="o">=</span><span class="n">db_constraint</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_null_arg</span> <span class="o">=</span> <span class="s1">&#39;null&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span>

        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="o">=</span> <span class="n">db_table</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">swappable</span> <span class="o">=</span> <span class="n">swappable</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="o">*</span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_unique</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_relationship_model</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_ignored_options</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_check_table_uniqueness</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
        <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_check_unique</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s1">&#39;ManyToManyFields cannot be unique.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E330&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_check_ignored_options</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">warnings</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_null_arg</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Warning</span><span class="p">(</span>
                    <span class="s1">&#39;null has no effect on ManyToManyField.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.W340&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_validators</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Warning</span><span class="p">(</span>
                    <span class="s1">&#39;ManyToManyField does not support validators.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.W341&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">limit_choices_to</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Warning</span><span class="p">(</span>
                    <span class="s1">&#39;limit_choices_to has no effect on ManyToManyField &#39;</span>
                    <span class="s1">&#39;with a through model.&#39;</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.W343&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">warnings</span>

    <span class="k">def</span> <span class="nf">_check_relationship_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">):</span>
            <span class="n">qualified_model_name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">qualified_model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_auto_created</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="c1"># The relationship model is not installed.</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;Field specifies a many-to-many relation through model &quot;</span>
                    <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;, which has not been installed.&quot;</span> <span class="o">%</span> <span class="n">qualified_model_name</span><span class="p">,</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E331&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="n">from_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="p">(</span>
                <span class="s2">&quot;ManyToManyField with intermediate &quot;</span>
                <span class="s2">&quot;tables cannot be checked if you don&#39;t pass the model &quot;</span>
                <span class="s2">&quot;where the field is attached to.&quot;</span>
            <span class="p">)</span>
            <span class="c1"># Set some useful local variables</span>
            <span class="n">to_model</span> <span class="o">=</span> <span class="n">resolve_relation</span><span class="p">(</span><span class="n">from_model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
            <span class="n">from_model_name</span> <span class="o">=</span> <span class="n">from_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to_model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="n">to_model_name</span> <span class="o">=</span> <span class="n">to_model</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">to_model_name</span> <span class="o">=</span> <span class="n">to_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="n">relationship_model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="n">self_referential</span> <span class="o">=</span> <span class="n">from_model</span> <span class="o">==</span> <span class="n">to_model</span>

            <span class="c1"># Check symmetrical attribute.</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">self_referential</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">symmetrical</span> <span class="ow">and</span>
                    <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">):</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s1">&#39;Many-to-many fields with intermediate tables must not be symmetrical.&#39;</span><span class="p">,</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E332&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="c1"># Count foreign keys in intermediate model</span>
            <span class="k">if</span> <span class="n">self_referential</span><span class="p">:</span>
                <span class="n">seen_self</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span>
                    <span class="n">from_model</span> <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span>
                <span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_self</span> <span class="o">&gt;</span> <span class="mi">2</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;The model is used as an intermediate model by &quot;</span>
                            <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;, but it has more than two foreign keys &quot;</span>
                            <span class="s2">&quot;to &#39;</span><span class="si">%s</span><span class="s2">&#39;, which is ambiguous. You must specify &quot;</span>
                            <span class="s2">&quot;which two foreign keys Django should use via the &quot;</span>
                            <span class="s2">&quot;through_fields keyword argument.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_model_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;Use through_fields to specify which two foreign keys Django should use.&quot;</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E333&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Count foreign keys in relationship model</span>
                <span class="n">seen_from</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span>
                    <span class="n">from_model</span> <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span>
                <span class="p">)</span>
                <span class="n">seen_to</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span>
                    <span class="n">to_model</span> <span class="o">==</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span>
                <span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_from</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="p">(</span><span class="s2">&quot;The model is used as an intermediate model by &quot;</span>
                             <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;, but it has more than one foreign key &quot;</span>
                             <span class="s2">&quot;from &#39;</span><span class="si">%s</span><span class="s2">&#39;, which is ambiguous. You must specify &quot;</span>
                             <span class="s2">&quot;which foreign key Django should use via the &quot;</span>
                             <span class="s2">&quot;through_fields keyword argument.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_model_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="p">(</span>
                                <span class="s1">&#39;If you want to create a recursive relationship, &#39;</span>
                                <span class="s1">&#39;use ForeignKey(&quot;self&quot;, symmetrical=False, through=&quot;</span><span class="si">%s</span><span class="s1">&quot;).&#39;</span>
                            <span class="p">)</span> <span class="o">%</span> <span class="n">relationship_model_name</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E334&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_to</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;The model is used as an intermediate model by &quot;</span>
                            <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;, but it has more than one foreign key &quot;</span>
                            <span class="s2">&quot;to &#39;</span><span class="si">%s</span><span class="s2">&#39;, which is ambiguous. You must specify &quot;</span>
                            <span class="s2">&quot;which foreign key Django should use via the &quot;</span>
                            <span class="s2">&quot;through_fields keyword argument.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_model_name</span><span class="p">),</span>
                            <span class="n">hint</span><span class="o">=</span><span class="p">(</span>
                                <span class="s1">&#39;If you want to create a recursive relationship, &#39;</span>
                                <span class="s1">&#39;use ForeignKey(&quot;self&quot;, symmetrical=False, through=&quot;</span><span class="si">%s</span><span class="s1">&quot;).&#39;</span>
                            <span class="p">)</span> <span class="o">%</span> <span class="n">relationship_model_name</span><span class="p">,</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E335&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

                <span class="k">if</span> <span class="n">seen_from</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">seen_to</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                            <span class="s2">&quot;The model is used as an intermediate model by &quot;</span>
                            <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39;, but it does not have a foreign key to &#39;</span><span class="si">%s</span><span class="s2">&#39; or &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                <span class="bp">self</span><span class="p">,</span> <span class="n">from_model_name</span><span class="p">,</span> <span class="n">to_model_name</span>
                            <span class="p">),</span>
                            <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
                            <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E336&#39;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>

        <span class="c1"># Validate `through_fields`.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Validate that we&#39;re given an iterable of at least two items</span>
            <span class="c1"># and that none of them is &quot;falsy&quot;.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">2</span> <span class="ow">and</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                        <span class="s2">&quot;Field specifies &#39;through_fields&#39; but does not provide &quot;</span>
                        <span class="s2">&quot;the names of the two link fields that should be used &quot;</span>
                        <span class="s2">&quot;for the relation through model &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">qualified_model_name</span><span class="p">,</span>
                        <span class="n">hint</span><span class="o">=</span><span class="s2">&quot;Make sure you specify &#39;through_fields&#39; as through_fields=(&#39;field1&#39;, &#39;field2&#39;)&quot;</span><span class="p">,</span>
                        <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                        <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E337&#39;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>

            <span class="c1"># Validate the given through fields -- they should be actual</span>
            <span class="c1"># fields on the through model, and also be foreign keys to the</span>
            <span class="c1"># expected models.</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">assert</span> <span class="n">from_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="p">(</span>
                    <span class="s2">&quot;ManyToManyField with intermediate &quot;</span>
                    <span class="s2">&quot;tables cannot be checked if you don&#39;t pass the model &quot;</span>
                    <span class="s2">&quot;where the field is attached to.&quot;</span>
                <span class="p">)</span>

                <span class="n">source</span><span class="p">,</span> <span class="n">through</span><span class="p">,</span> <span class="n">target</span> <span class="o">=</span> <span class="n">from_model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span>
                <span class="n">source_field_name</span><span class="p">,</span> <span class="n">target_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>

                <span class="k">for</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">related_model</span> <span class="ow">in</span> <span class="p">((</span><span class="n">source_field_name</span><span class="p">,</span> <span class="n">source</span><span class="p">),</span>
                                                  <span class="p">(</span><span class="n">target_field_name</span><span class="p">,</span> <span class="n">target</span><span class="p">)):</span>

                    <span class="n">possible_field_names</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;remote_field&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="o">==</span> <span class="n">related_model</span><span class="p">:</span>
                            <span class="n">possible_field_names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">possible_field_names</span><span class="p">:</span>
                        <span class="n">hint</span> <span class="o">=</span> <span class="s2">&quot;Did you mean one of the following foreign keys to &#39;</span><span class="si">%s</span><span class="s2">&#39;: </span><span class="si">%s</span><span class="s2">?&quot;</span> <span class="o">%</span> <span class="p">(</span>
                            <span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                            <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">possible_field_names</span><span class="p">),</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">hint</span> <span class="o">=</span> <span class="kc">None</span>

                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">field</span> <span class="o">=</span> <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
                    <span class="k">except</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldDoesNotExist</span><span class="p">:</span>
                        <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                                <span class="s2">&quot;The intermediary model &#39;</span><span class="si">%s</span><span class="s2">&#39; has no field &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span>
                                <span class="o">%</span> <span class="p">(</span><span class="n">qualified_model_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">),</span>
                                <span class="n">hint</span><span class="o">=</span><span class="n">hint</span><span class="p">,</span>
                                <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                                <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E338&#39;</span><span class="p">,</span>
                            <span class="p">)</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;remote_field&#39;</span><span class="p">)</span> <span class="ow">and</span>
                                <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;model&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="o">==</span> <span class="n">related_model</span><span class="p">):</span>
                            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                                    <span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&#39; is not a foreign key to &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                                        <span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span>
                                        <span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                                    <span class="p">),</span>
                                    <span class="n">hint</span><span class="o">=</span><span class="n">hint</span><span class="p">,</span>
                                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E339&#39;</span><span class="p">,</span>
                                <span class="p">)</span>
                            <span class="p">)</span>

        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">_check_table_uniqueness</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">registered_tables</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">:</span> <span class="n">model</span>
            <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_auto_created</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">model</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">and</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">managed</span>
        <span class="p">}</span>
        <span class="n">m2m_db_table</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">m2m_db_table</span><span class="p">()</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">registered_tables</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">m2m_db_table</span><span class="p">)</span>
        <span class="c1"># The second condition allows multiple m2m relations on a model if</span>
        <span class="c1"># some point to a through model that proxies another through model.</span>
        <span class="k">if</span> <span class="n">model</span> <span class="ow">and</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="k">def</span> <span class="nf">_get_field_name</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">is</span> <span class="n">model</span><span class="p">:</span>
                            <span class="k">return</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="o">.</span><span class="n">_meta</span>
                <span class="n">clashing_obj</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">label</span><span class="p">,</span> <span class="n">_get_field_name</span><span class="p">(</span><span class="n">model</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">clashing_obj</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">checks</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span>
                    <span class="s2">&quot;The field&#39;s intermediary table &#39;</span><span class="si">%s</span><span class="s2">&#39; clashes with the &quot;</span>
                    <span class="s2">&quot;table name of &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">m2m_db_table</span><span class="p">,</span> <span class="n">clashing_obj</span><span class="p">),</span>
                    <span class="n">obj</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                    <span class="nb">id</span><span class="o">=</span><span class="s1">&#39;fields.E340&#39;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="k">return</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">deconstruct</span><span class="p">()</span>
        <span class="c1"># Handle the simpler arguments.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;db_table&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">db_constraint</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;db_constraint&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">db_constraint</span>
        <span class="c1"># Rel needs more work.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;through&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;through&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;through&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                <span class="p">)</span>
        <span class="c1"># If swappable is True, then see if we&#39;re actually pointing to the target</span>
        <span class="c1"># of a swap.</span>
        <span class="n">swappable_setting</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">swappable_setting</span>
        <span class="k">if</span> <span class="n">swappable_setting</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># If it&#39;s already a settings reference, error.</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">],</span> <span class="s2">&quot;setting_name&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span> <span class="o">!=</span> <span class="n">swappable_setting</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;Cannot deconstruct a ManyToManyField pointing to a &quot;</span>
                        <span class="s2">&quot;model that is swapped in place of more than one model &quot;</span>
                        <span class="s2">&quot;(</span><span class="si">%s</span><span class="s2"> and </span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">setting_name</span><span class="p">,</span> <span class="n">swappable_setting</span><span class="p">)</span>
                    <span class="p">)</span>

            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">SettingsReference</span><span class="p">(</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">],</span>
                <span class="n">swappable_setting</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">_get_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">direct</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Called by both direct and indirect m2m traversal.&quot;&quot;&quot;</span>
        <span class="n">int_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span>
        <span class="n">linkfield1</span> <span class="o">=</span> <span class="n">int_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">m2m_field_name</span><span class="p">())</span>
        <span class="n">linkfield2</span> <span class="o">=</span> <span class="n">int_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">direct</span><span class="p">:</span>
            <span class="n">join1infos</span> <span class="o">=</span> <span class="n">linkfield1</span><span class="o">.</span><span class="n">get_reverse_path_info</span><span class="p">()</span>
            <span class="n">join2infos</span> <span class="o">=</span> <span class="n">linkfield2</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">(</span><span class="n">filtered_relation</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">join1infos</span> <span class="o">=</span> <span class="n">linkfield2</span><span class="o">.</span><span class="n">get_reverse_path_info</span><span class="p">()</span>
            <span class="n">join2infos</span> <span class="o">=</span> <span class="n">linkfield1</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">(</span><span class="n">filtered_relation</span><span class="p">)</span>

        <span class="c1"># Get join infos between the last model of join 1 and the first model</span>
        <span class="c1"># of join 2. Assume the only reason these may differ is due to model</span>
        <span class="c1"># inheritance.</span>
        <span class="n">join1_final</span> <span class="o">=</span> <span class="n">join1infos</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">to_opts</span>
        <span class="n">join2_initial</span> <span class="o">=</span> <span class="n">join2infos</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">from_opts</span>
        <span class="k">if</span> <span class="n">join1_final</span> <span class="ow">is</span> <span class="n">join2_initial</span><span class="p">:</span>
            <span class="n">intermediate_infos</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">elif</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">join1_final</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">join2_initial</span><span class="o">.</span><span class="n">model</span><span class="p">):</span>
            <span class="n">intermediate_infos</span> <span class="o">=</span> <span class="n">join1_final</span><span class="o">.</span><span class="n">get_path_to_parent</span><span class="p">(</span><span class="n">join2_initial</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">intermediate_infos</span> <span class="o">=</span> <span class="n">join2_initial</span><span class="o">.</span><span class="n">get_path_from_parent</span><span class="p">(</span><span class="n">join1_final</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">[</span><span class="o">*</span><span class="n">join1infos</span><span class="p">,</span> <span class="o">*</span><span class="n">intermediate_infos</span><span class="p">,</span> <span class="o">*</span><span class="n">join2infos</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_path_info</span><span class="p">(</span><span class="n">direct</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="n">filtered_relation</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_reverse_path_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_path_info</span><span class="p">(</span><span class="n">direct</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">filtered_relation</span><span class="o">=</span><span class="n">filtered_relation</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_m2m_db_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">opts</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Function that can be curried to provide the m2m table name for this</span>
<span class="sd">        relation.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">db_table</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">m2m_table_name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">utils</span><span class="o">.</span><span class="n">strip_quotes</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">db_table</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">utils</span><span class="o">.</span><span class="n">truncate_name</span><span class="p">(</span><span class="n">m2m_table_name</span><span class="p">,</span> <span class="n">connection</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">max_name_length</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">_get_m2m_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Function that can be curried to provide the source accessor or DB</span>
<span class="sd">        column name for the m2m table.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cache_attr</span> <span class="o">=</span> <span class="s1">&#39;_m2m_</span><span class="si">%s</span><span class="s1">_cache&#39;</span> <span class="o">%</span> <span class="n">attr</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="n">related</span><span class="o">.</span><span class="n">related_model</span> <span class="ow">and</span>
                    <span class="p">(</span><span class="n">link_field_name</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">link_field_name</span> <span class="o">==</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)):</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
                <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_m2m_reverse_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Function that can be curried to provide the related accessor or DB</span>
<span class="sd">        column name for the m2m table.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cache_attr</span> <span class="o">=</span> <span class="s1">&#39;_m2m_reverse_</span><span class="si">%s</span><span class="s1">_cache&#39;</span> <span class="o">%</span> <span class="n">attr</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>
        <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through_fields</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">link_field_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">link_field_name</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">related</span><span class="o">.</span><span class="n">related_model</span> <span class="o">==</span> <span class="n">related</span><span class="o">.</span><span class="n">model</span><span class="p">:</span>
                    <span class="c1"># If this is an m2m-intermediate to self,</span>
                    <span class="c1"># the first foreign key you find will be</span>
                    <span class="c1"># the source column. Keep searching for</span>
                    <span class="c1"># the second foreign key.</span>
                    <span class="k">if</span> <span class="n">found</span><span class="p">:</span>
                        <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
                        <span class="k">break</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">elif</span> <span class="n">link_field_name</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">link_field_name</span> <span class="o">==</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache_attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># To support multiple relations to self, it&#39;s useful to have a non-None</span>
        <span class="c1"># related name on symmetrical relations for internal reasons. The</span>
        <span class="c1"># concept doesn&#39;t make a lot of sense externally (&quot;you want me to</span>
        <span class="c1"># specify *what* on my non-reversible relation?!&quot;), so we set it up</span>
        <span class="c1"># automatically. The funky name reduces the chance of an accidental</span>
        <span class="c1"># clash.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">symmetrical</span> <span class="ow">and</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="s2">&quot;self&quot;</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">_rel_+&quot;</span> <span class="o">%</span> <span class="n">name</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">():</span>
            <span class="c1"># If the backwards relation is disabled, replace the original</span>
            <span class="c1"># related_name with one generated from the m2m field name. Django</span>
            <span class="c1"># still uses backwards relations internally and we need to avoid</span>
            <span class="c1"># clashes between multiple m2m fields with related_name == &#39;+&#39;.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">related_name</span> <span class="o">=</span> <span class="s2">&quot;_</span><span class="si">%s</span><span class="s2">_</span><span class="si">%s</span><span class="s2">_+&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">name</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">contribute_to_class</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># The intermediate m2m model is not auto created if:</span>
        <span class="c1">#  1) There is a manually specified intermediate, or</span>
        <span class="c1">#  2) The class owning the m2m field is abstract.</span>
        <span class="c1">#  3) The class owning the m2m field has been swapped out.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">abstract</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">:</span>
                <span class="k">def</span> <span class="nf">resolve_through_model</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">field</span><span class="p">):</span>
                    <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">model</span>
                <span class="n">lazy_related_operation</span><span class="p">(</span><span class="n">resolve_through_model</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span> <span class="n">field</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">create_many_to_many_intermediary_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span>

        <span class="c1"># Add the descriptor for the m2m relation.</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">ManyToManyDescriptor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>

        <span class="c1"># Set up the accessor for the m2m table name for the relation.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_db_table</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_db_table</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_meta</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">contribute_to_related_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">cls</span><span class="p">,</span> <span class="n">related</span><span class="p">):</span>
        <span class="c1"># Internal M2Ms (i.e., those with a related name ending with &#39;+&#39;)</span>
        <span class="c1"># and swapped models don&#39;t get a related descriptor.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">related</span><span class="o">.</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">related</span><span class="o">.</span><span class="n">get_accessor_name</span><span class="p">(),</span> <span class="n">ManyToManyDescriptor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>

        <span class="c1"># Set up the accessors for the column names on the m2m table.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_column_name</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s1">&#39;column&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_name</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_reverse_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s1">&#39;column&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_field_name</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_field_name</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_reverse_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">)</span>

        <span class="n">get_m2m_rel</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s1">&#39;remote_field&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_target_field_name</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">get_m2m_rel</span><span class="p">()</span><span class="o">.</span><span class="n">field_name</span>
        <span class="n">get_m2m_reverse_rel</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_m2m_reverse_attr</span><span class="p">,</span> <span class="n">related</span><span class="p">,</span> <span class="s1">&#39;remote_field&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">m2m_reverse_target_field_name</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">get_m2m_reverse_rel</span><span class="p">()</span><span class="o">.</span><span class="n">field_name</span>

    <span class="k">def</span> <span class="nf">set_attributes_from_rel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">value_from_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="nb">list</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">save_form_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;form_class&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelMultipleChoiceField</span><span class="p">,</span>
            <span class="s1">&#39;queryset&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">using</span><span class="p">),</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="c1"># If initial is passed in, it&#39;s a list of related objects, but the</span>
        <span class="c1"># MultipleChoiceField takes a list of IDs.</span>
        <span class="k">if</span> <span class="n">defaults</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;initial&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">initial</span> <span class="o">=</span> <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;initial&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">initial</span><span class="p">):</span>
                <span class="n">initial</span> <span class="o">=</span> <span class="n">initial</span><span class="p">()</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;initial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">pk</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">initial</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">db_check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="c1"># A ManyToManyField is not represented by a single column,</span>
        <span class="c1"># so return None.</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">db_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;check&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">}</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>